package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2018/4/13.
 */

public class RecommendTopWalletV2View extends LinearLayout {

    private TextView topWalletNumTx;
    private ImageView topWalletIv;
    private LinearLayout topWalletRootLl;

    public RecommendTopWalletV2View(Context context) {
        super(context);
        initView(context);
    }

    public RecommendTopWalletV2View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.part_top_wallet_v2_view, null);
        topWalletRootLl = rootView.findViewById(R.id.topWalletRootLl);
        topWalletIv = rootView.findViewById(R.id.topWalletIv);
        topWalletNumTx = rootView.findViewById(R.id.topWalletNumTx);

        setBackgroundAlpha(0);
        int w = (int) getContext().getResources().getDimension(R.dimen.wdp68);
        int h = (int) getContext().getResources().getDimension(R.dimen.wdp70);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(w, h);
        topWalletRootLl.setLayoutParams(params);

        this.removeAllViews();
        this.addView(rootView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AccountUtil.getInstance().getUidNotToLogin(getContext()) == 0) return;
                IntentUtil.getInstance().goShareWebActivity(getContext(), MyApplication.cfgPlug.getValue_wallet_url_v2());
            }
        });

        initTotalWalletNum();
    }

    //0:全透明，255：不透明
    public void setBackgroundAlpha(@IntRange(from=0,to=255)int alpha) {
        Drawable collBg = topWalletRootLl.getBackground();
        collBg.setAlpha(alpha);
        topWalletRootLl.setBackgroundDrawable(collBg);
        if (alpha==0) {
            topWalletIv.setImageResource(R.mipmap.ic_top_wallet_v2);
        } else {
            topWalletIv.setImageResource(R.mipmap.ic_top_wallet_v2_yy);
        }
        invalidate();
    }

    public void initTotalWalletNum() {
        try {
            AccountBean accountBean = AccountUtil.getInstance().getAccountData();
            if (accountBean==null) {//未登陆
                setTotalNum("0.00");
            } else {
                int temp = accountBean.getWallet();
                if (temp<=0){
                    setTotalNum("0.00");
                } else {
                    float money = temp/100f;
                    setTotalNum(money+"");
                }
            }
        } catch (Exception e) {
            LogUtils.e("RecommendTopDownloadV2View>>>>e:"+e.getMessage());
        }
    }

    public void setTotalNum(String totalDownloadNum) {
        topWalletNumTx.setText(totalDownloadNum);
        invalidate();
    }
}
